package com.example.forum.services.impl;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.dao.MessageMapper;
import com.example.forum.dao.UserMapper;
import com.example.forum.exception.ApplicationException;
import com.example.forum.model.Message;
import com.example.forum.model.User;
import com.example.forum.services.IMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author: wenjingyuan
 * @Date: 2023/10/09/9:20
 * @Description:
 */
@Slf4j
@Service
public class MessageServiceImpl implements IMessageService {

    @Resource
    private MessageMapper messageMapper;
    @Resource
    private UserMapper userMapper;
    @Override
    public void create(Message message) {
        //非空检验
        if(message==null || message.getPostUserId()==null || message.getReceiveUserId()==null
        || StringUtils.isEmpty(message.getContent())){
            //打印日志
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //检验接受者是否存在
        User user = userMapper.selectByPrimaryKey(message.getReceiveUserId());
        if(user==null ||user.getDeleteState()==1){
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //设置默认值
        message.setState((byte) 0);
        message.setDeleteState((byte) 0);
        Date date=new Date();
        message.setCreateTime(date);
        message.setUpdateTime(date);
        //调用DAO
        int row = messageMapper.insertSelective(message);
        if(row!=1){
            //打印日志
            log.warn(ResultCode.FAILED.toString());
            //抛出异常
            throw  new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }

    @Override
    public Message selectById(Long id) {
        if(id ==null ||id<=0){
            //打印日志
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //调用DAO
        Message message = messageMapper.selectByPrimaryKey(id);
        return message;
    }

    @Override
    public Integer selectUnreadCount(Long receiveUserId) {
        if(receiveUserId==null ||receiveUserId<=0){
            //打印日志
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //调用DAO
        Integer count = messageMapper.selectUnreadCount(receiveUserId);
        if(count==null){
            //打印日志
            log.warn(ResultCode.ERROR_SERVICES.toString());
            //抛出异常
            throw  new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
        return count;
    }

    @Override
    public List<Message> selectByReceiveUserId(Long receiveUserId) {
        if(receiveUserId==null ||receiveUserId<=0){
            //打印日志
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //调用DAO
        List<Message> messages = messageMapper.selectByReceiveUserId(receiveUserId);
        return messages;
    }

    @Override
    public void updateState(Long id, Byte state) {
        if(id==null ||id<=0||state<0||state>2){
            //打印日志
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //构造对象
        Message message=new Message();
        message.setId(id);
        message.setState(state);
        Date date=new Date();
        message.setUpdateTime(date);

        //调用DAO
        int row = messageMapper.updateByPrimaryKeySelective(message);
        if(row!=1){
            //打印日志
            log.warn(ResultCode.ERROR_SERVICES.toString());
            //抛出异常
            throw  new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
    }

    @Override
    public void reply(Long repliedId, Message message) {
        //检验参数
        if(repliedId==null ||repliedId<=0){
            //打印日志
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Message existMessage = messageMapper.selectByPrimaryKey(repliedId);
        //站内信不存在
        if(existMessage==null || existMessage.getDeleteState()==1){
            //打印日志
            log.info(ResultCode.FAILED_MESSAGE_NOT_EXISTS.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_MESSAGE_NOT_EXISTS));
        }
        //更新状态为已回复
        updateState(repliedId, (byte) 2);
        //回复的内容写入到数据库
        create(message);
    }
}
